using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Rss;

namespace MSNSearch.Activities
{
    [Designer(typeof(ConsoleActivityDesigner), typeof(IDesigner))]
    [ToolboxBitmap(typeof(ConvertSearchResponseToRssFeed), "Resources.Custom Activity Background - Live Expo.jpg")]
    public partial class ConvertSearchResponseToRssFeed : Activity
	{
		public ConvertSearchResponseToRssFeed()
		{
			InitializeComponent();
		}

        public static DependencyProperty SearchResponseToConvertProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SearchResponseToConvert", typeof(SearchResponse), typeof(ConvertSearchResponseToRssFeed));
        [Description("This is the search response object to convert to an RSS Feed.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SearchResponse SearchResponseToConvert
        {
            get
            {
                return ((SearchResponse)(base.GetValue(ConvertSearchResponseToRssFeed.SearchResponseToConvertProperty)));
            }
            set
            {

                base.SetValue(ConvertSearchResponseToRssFeed.SearchResponseToConvertProperty, value);

            }
        }

        public static DependencyProperty TitleProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Title", typeof(string), typeof(ConvertSearchResponseToRssFeed));
        [Description("This is the title to associate with the Feed.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Title
        {
            get
            {
                return ((string)(base.GetValue(ConvertSearchResponseToRssFeed.TitleProperty)));
            }
            set
            {

                base.SetValue(ConvertSearchResponseToRssFeed.TitleProperty, value);

            }
        }


        public static DependencyProperty ResultProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Result", typeof(RssFeed), typeof(ConvertSearchResponseToRssFeed));
        [Description("The response from the search.")]
        [Category("Result")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public RssFeed Result
        {
            get
            {
                return ((RssFeed)(base.GetValue(ConvertSearchResponseToRssFeed.ResultProperty)));
            }
            set
            {
                base.SetValue(ConvertSearchResponseToRssFeed.ResultProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            RssFeed rssFeed = new RssFeed();
            RssChannel channel = new RssChannel();
            channel.Title = Title;
            channel.LastBuildDate = System.DateTime.Now.ToString();
            channel.LastWriteTime = System.DateTime.Now.ToString();

            foreach (SourceResponse sr in SearchResponseToConvert.Responses)
            {
                foreach (Result result in sr.Results)
                {
                    RssItem item = new RssItem();
                    if (result.Title !=null) {item.Title = result.Title;}
                    if (result.Description != null) {item.Description = result.Description;};
                    if (result.DateTime != null) {  item.PubDate = result.DateTime.ToString();};
                    if (result.Url !=null) {item.Link = result.Url;}
                    if (result.Url !=null) {item.XmlUrl = result.Url;};
                    item.OriginatedFrom = "LiveSearch";
                    if (result.SearchTagsArray != null)
                    {
                        foreach (SearchTag category in result.SearchTagsArray)
                        {
                            RssCategory rssCategory = new RssCategory();
                            if (category.Name != null) { rssCategory.Category = category.Name; };
                            if (category.Value != null) { rssCategory.Category = category.Value; };
                            item.Categories.Add(rssCategory);

                        }
                    }
                        channel.Items.Add(item);
                }

            }
            rssFeed.Channels.Add(channel);


            Result = rssFeed;
            //
            // NOTE: Use static method to trigger system to start up
            //

            return base.Execute(executionContext);

        }
    
    }
}
